package com.zhaoc.web.action;

import com.zhaoc.domain.take_delivery.Promotion;
import com.zhaoc.service.IPromotionService;
import com.zhaoc.web.base.BaseAction;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/12 0012
 * time:18:09
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionFormAction extends BaseAction<Promotion> {

    //使用模型驱动
    private File titleImgFile;
    private String titleImgFileFileName;
    //获取service层对象
    @Autowired
    private IPromotionService promotionService;

    public void setTitleImgFile(File titleImgFile) {
        this.titleImgFile = titleImgFile;
    }

    public void setTitleImgFileFileName(String titleImgFileFileName) {
        this.titleImgFileFileName = titleImgFileFileName;
    }

    @Action(value = "promotion_save", results = {
            @Result(name = SUCCESS, type = "redirect", location = "./pages/take_delivery/promotion.html")})
    public String promotionSave() throws IOException {
        System.out.println("-------------------------------------------");
        //宣传图的保存路径及上传 路径
        String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
        String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";

        //生成随机图片名
        UUID uuid = UUID.randomUUID();
        String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
        String randomName = uuid + ext;

        //以绝对路径保存图片
        File destFile = new File(savePath + "/" + randomName);
        System.out.println(destFile.getAbsolutePath());
        FileUtils.copyFile(titleImgFile, destFile);

        //将保存路径相对工程的web访问路径,保存在model工程中
        model.setTitleImg(ServletActionContext.getRequest().getContextPath() + "/upload/" + randomName);

        //调用业务层完成活动的保存
        promotionService.save(model);


        return SUCCESS;
    }


    @Action(value = "promotion_pageQuery", results = {@Result(name = SUCCESS, type = "json")})
    public String pageQuery() {
        //查询所有活动
        //构造分页查询参数
        Pageable pageable = new PageRequest(page - 1, rows);
        //调用业务层查询
        Page<Promotion> pageDate = promotionService.findPageData(pageable);
        //将查到的数据压在栈顶回显
        pushPageDataToValueStack(pageDate);
        return SUCCESS;
    }


}
